package webdev.danagtravel;

import java.util.ArrayList;

import webdev.common.Common;
import webdev.database.SQLQuery;
import webdev.dto.UserDTO;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import dev.danang.R;

public class SignUpActivity extends Activity implements OnClickListener {

	public final static String TAG = SignUpActivity.class.getSimpleName();
	
	private TextView mHeader;
	private ImageView mBack;
	private EditText mTxtUserName;
	private EditText mTxtPassword;
	private EditText mTxtRetypePassword;

	// button sign up
	private Button mSignUp;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// set screen portrait
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		setContentView(R.layout.sign_up);
		init();
		setValue();
		setListener();
	}
	private void init() {
		mHeader = (TextView) findViewById(R.id.text_view_header_title);
		mBack = (ImageView) findViewById(R.id.img_back);
		
		mTxtUserName = (EditText) findViewById(R.id.txt_user_name);
		mTxtPassword = (EditText) findViewById(R.id.txt_password);
		mTxtRetypePassword = (EditText) findViewById(R.id.txt_retype_password);
		mSignUp = (Button) findViewById(R.id.btn_sign_up);
		
//		mProgressBar = (ProgressBar) findViewById(R.id.progressbar_sign_up);
	}
	
	private void setValue() {
		mHeader.setText("Tạo tài khoản");
	}
	
	private void setListener() {
		mBack.setOnClickListener(this);
		mSignUp.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		Intent i;
		switch (v.getId()) {
		case R.id.img_back:
			i = new Intent(SignUpActivity.this, WellcomeActivity.class);
			startActivity(i);
			SignUpActivity.this.finish();
			break;
		case R.id.btn_sign_up:
			if (createAccount()) {
				i = new Intent(SignUpActivity.this, MainActivity.class);
				startActivity(i);
			}
			break;
		default:
			break;
		}
		
	}

	private boolean createAccount() {
		boolean isCreateAccountOk = false;
		String userName = mTxtUserName.getText().toString();
		String password = mTxtPassword.getText().toString();
		String retypePassword = mTxtRetypePassword.getText().toString();

		if ("".equals(userName)) {
			Toast.makeText(this, "Nhập tên tài khoản", Toast.LENGTH_SHORT).show();
			return isCreateAccountOk;
		}
		if ("".equals(password)) {
			Toast.makeText(this, "Nhập mật khẩu", Toast.LENGTH_SHORT).show();
			return isCreateAccountOk;
		}
		if ("".equals(retypePassword)) {
			Toast.makeText(this, "Nhập lại mật khẩu", Toast.LENGTH_SHORT).show();
			return isCreateAccountOk;
		}
		if ( (password.equals(retypePassword)) == false ) {
			Toast.makeText(this, "Mật khẩu không trùng nhau", Toast.LENGTH_SHORT).show();
			return isCreateAccountOk;
		}


		SQLQuery db = new SQLQuery(this);
		ArrayList<UserDTO> listUser = db.getAllUser();
		
		boolean isUserOk = true;
		int size = listUser.size();
		String currentUserName;
		for (int i = 0; i < size; i++) {
			currentUserName = listUser.get(i).getmUserName();
			if (userName.equals(currentUserName)) {
				isUserOk = false;
				break;
			}
		} // end for loop

		if (isUserOk) {
			// Add user to db
			Common.currentUser = new UserDTO(userName, retypePassword);
			isCreateAccountOk = db.addUser(Common.currentUser);
			if (isCreateAccountOk) {
				Toast.makeText(this, "Tạo tài khoản thành công", Toast.LENGTH_SHORT).show();
			} else {
				Toast.makeText(this, "Tạo tài khoản không thành công", Toast.LENGTH_SHORT).show();
			}
		} else {
			Toast.makeText(this, "Tên tài khoản này đã có rồi", Toast.LENGTH_SHORT).show();
		}

		return isCreateAccountOk;
	}

	public void onBackPressed() {
        Log.d("CDA", "onBackPressed Called");
        Intent setIntent = new Intent(SignUpActivity.this, WellcomeActivity.class);
        startActivity(setIntent);
        SignUpActivity.this.finish();
        return;
    }
	
}
